1538C - Number of Pairs - CodeForces Solution


binary search data structures math two pointers *1300

Please click on ads to support us..

Python Code:

q = int(input())
for i in range(q):
    n ,l , r= map(int , input().split())
    a = list(map(int , input().split()))
    a.sort()
    poi1 = 0
    poi2 = n-1
    ans = 0
    while (poi1 < poi2):
        if a[poi2]+a[poi1] < r+1:
            ans+=(poi2 - poi1)
            poi1+=1
        else:
            poi2-=1
    poi3 = 0
    poi4 = n-1
    ans1 = 0
    while (poi3 < poi4):
        if a[poi3]+a[poi4] < l:
            ans1+=(poi4 - poi3)
            poi3+= 1
        else:
            poi4-=1
    print(ans - ans1)

C++ Code:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int a[200005];

void solve() {
    int n, l, r;
    ll ans = 0;
    cin >> n >> l >> r;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    
    sort(a + 1, a + n + 1);
    
    for (int i = 1; i <= n - 1; ++i) {
        int l1 = i + 1, r1 = n, mid1 = l1;
        while (l1 < r1) {
            int mid1 = (l1 + r1) >> 1;
            if (l <= a[i] + a[mid1] && a[i] + a[mid1] <= r) {
                r1 = mid1;
            } else if (a[i] + a[mid1] < l) {
                l1 = mid1 + 1;
            } else {
                r1 = mid1;
            }
        }
        
        int l2 = i + 1, r2 = n, mid2 = l2;
        while (l2 < r2) {
            mid2 = (l2 + r2 + 1) >> 1;
            if (l <= a[i] + a[mid2] && a[i] + a[mid2] <= r) {
                l2 = mid2;
            } else if (a[i] + a[mid2] < l) {
                l2 = mid2;
            } else {
                r2 = mid2 - 1;
            }
        }
        
        if (l <= a[i] + a[l1] && a[i] + a[l1] <= r) ans += (ll)l2 - (ll)l1 + 1;
    }
    
    printf("%lld\n", ans);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
	return 0;
}


Comments

Submit
0 Comments
More Questions

1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle